﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Windows.Forms;
using System.Drawing;

namespace NutriStat
{
    /// <summary>
    /// Data type for the configuration object. This is used to store and retrieve configuration
    /// settings for the NutriStat application. Creating an instance of the object automatically
    /// loads the settings from the configuration XML file into the object's data members.
    /// </summary>
    public class NutriConfig
    {
        #region Data
        #region Protected Data
        #endregion Protected Data

        #region Private Data
        private string referenceType;
        private string measurementSystem;
        private string cultureCode;

        // This is needed to bypass a problem with the reference MDB files. When the user
        // opens a table for data entry, the current directory C# references is changed to
        // the directory of the file that was opened. This is a problem because the reference
        // MDBs are not necessarily in that same directory. If they aren't, then no statistics
        // are generated. We need a way to store the current working directory so that it can
        // be restored during run-time immediately after a table read.
        private static string currentDirectory;
        private int precisionZ;
        private int precisionC;
        private int precisionM;

        private string colorCoding;

        // other settings
        private bool deleteButton;
        private bool surveyFields;

        // Standard fields
        private bool name;
        private bool birthdate;
        private bool age;
        private bool mother;
        private bool father;
        private bool address;
        private bool height;
        private bool weight;
        private bool edema;
        private bool headCircumference;
        private bool armCircumference;
        private bool subscapularSkinFold;
        private bool tricepsSkinFold;        
        private bool notes;

        // Results
        private bool resultsHA;
        private bool resultsWA;
        private bool resultsWH;
        private bool resultsBMI;
        private bool resultsHead;
        private bool resultsArm;
        private bool resultsSSF;
        private bool resultsTSF;
        private bool resultsFlag;
        private bool resultsLegacyArm;

        // statistics
        private bool statsPercentiles;
        private bool statsZScores;
        private bool statsPercentOfMedian;

        // units
        private string unitsWeight;
        private string unitsHeight;
        private string unitsArm;
        private string unitsHead;
        private string unitsSkin;        
        #endregion Private Data

        // graphing
        private string colorP1;
        private string colorP3;
        private string colorP5;
        private string colorP10;
        private string colorP25;
        private string colorP50;
        private string colorP85;
        private int thickP1;
        private int thickP3;
        private int thickP5;
        private int thickP10;
        private int thickP25;
        private int thickP50;
        private int thickP85;

        private string color3SD;
        private string color2SD;
        private string color1SD;
        private string colorMedian;
        private string colorNormalCurve;
        private int thick3SD;
        private int thick2SD;
        private int thick1SD;
        private int thickMedian;
        private int thickNormalCurve;

        private string colorPatientCurve;
        private string colorPatientDataPointBorder;
        private string colorPatientDataPointFill;
        private int thickPatientCurve;
        private double thickPatientDataPointBorder;

        private int patientDataPointSize;
        private bool transparentDataPoints;

        private bool showLegend;
        private bool showDataPointLabels;
        private bool showCoordinateCursor;

        #region Internal Data
        internal List<string> recentFiles;
        #endregion Internal Data
        #endregion Data

        #region Constructors
        /// <summary>
        /// The constructor for the NutriStat Configuration object
        /// </summary>
        public NutriConfig()
        {
            CultureCode = "en-US";
            
            Name = true;
            Age = true;
            Mother = false;
            Father = false;
            Address = false;
            Height = true;
            Weight = true;

            ColorCoding = "1";
            NutriConfig.SetCurrentDirectory(Directory.GetCurrentDirectory());

            Edema = true;
            HeadCircumference = true;
            ArmCircumference = true;
            SubscapularSkinFold = true;
            TricepsSkinFold = true;
            Notes = true;

            ResultsHA = true;
            ResultsWA = true;
            ResultsWH = true;
            ResultsBMI = true;
            ResultsHead = true;
            ResultsArm = true;
            ResultsSSF = true;
            ResultsTSF = true;
            ResultsFlag = true;
            ResultsLegacyArm = false;

            StatsPercentiles = true;
            StatsZScores = true;
            StatsPercentOfMedian = false;
            
            UnitsWeight = "kg";
            UnitsHeight = SharedStrings.CENTIMETERS_ABBR;
            UnitsArm = SharedStrings.CENTIMETERS_ABBR;
            UnitsHead = SharedStrings.CENTIMETERS_ABBR;
            UnitsSkin = "mm";

            ReferenceType = "CDC 2000";

            PrecisionC = 2;
            PrecisionM = 2;
            PrecisionZ = 3;

            ColorP1 = Color.Transparent.Name;
            ColorP3 = Color.Red.Name;
            ColorP5 = Color.Orange.Name;
            ColorP10 = Color.Violet.Name;
            ColorP25 = Color.Green.Name;
            ColorP50 = Color.Blue.Name;
            ColorP85 = Color.Brown.Name;
            ThickP1 = 0;
            ThickP3 = 1;
            ThickP5 = 1;
            ThickP10 = 1;
            ThickP25 = 1;
            ThickP50 = 2;
            ThickP85 = 1;

            Color3SD = Color.Red.Name;
            Color2SD = Color.Orange.Name;
            Color1SD = Color.Green.Name;
            ColorMedian = Color.Blue.Name;
            ColorNormalCurve = Color.CornflowerBlue.Name;
            Thick3SD = 2;
            Thick2SD = 2;
            Thick1SD = 2;
            ThickMedian = 2;
            ThickNormalCurve = 2;

            ColorPatientCurve = Color.Black.Name;
            ColorPatientDataPointBorder = Color.Black.Name;
            ColorPatientDataPointFill = Color.Transparent.Name;
            ThickPatientCurve = 1;
            ThickPatientDataPointBorder = 1.7;

            PatientDataPointSize = 8;
            TransparentDataPoints = false;

            ShowLegend = true;
            ShowDataPointLabels = true;
            ShowCoordinateCursor = true;

            recentFiles = new List<string>();            
            LoadConfigFile();
        }

        /// <summary>
        /// The copy constructor for the NutriStat configuration object. Another configuration
        /// can be passed in and its values will be copied to the object being constructed.
        /// </summary>
        /// <param name="config">Another configuration.</param>        
        public NutriConfig(NutriConfig config)
        {
            CultureCode = config.CultureCode;

            Name = config.Name;
            Age = config.Age;
            Mother = config.Mother;
            Father = config.Father;
            Address = config.Address;
            Height = config.Height;
            Birthdate = config.Birthdate;
            Weight = config.Weight;

            NutriConfig.SetCurrentDirectory(Directory.GetCurrentDirectory());
            ColorCoding = config.ColorCoding;

            Edema = config.Edema;
            HeadCircumference = config.HeadCircumference;
            ArmCircumference = config.ArmCircumference;
            SubscapularSkinFold = config.SubscapularSkinFold;
            TricepsSkinFold = config.TricepsSkinFold;
            Notes = config.Notes;
            DeleteButton = config.DeleteButton;
            SurveyFields = config.SurveyFields;

            ResultsHA = config.ResultsHA;
            ResultsWA = config.ResultsWA;
            ResultsWH = config.ResultsWH;
            ResultsBMI = config.ResultsBMI;
            ResultsHead = config.ResultsHead;
            ResultsArm = config.ResultsArm;
            ResultsSSF = config.ResultsSSF;
            ResultsTSF = config.ResultsTSF;
            ResultsFlag = config.ResultsFlag;
            ResultsLegacyArm = config.ResultsLegacyArm;

            StatsPercentiles = config.StatsPercentiles;
            StatsZScores = config.StatsZScores;
            StatsPercentOfMedian = config.StatsPercentOfMedian;

            UnitsWeight = config.UnitsWeight;
            UnitsHeight = config.UnitsHeight;
            UnitsArm = config.UnitsArm;
            UnitsHead = config.UnitsHead;
            UnitsSkin = config.UnitsSkin;

            ReferenceType = config.ReferenceType;
            
            PrecisionC = config.PrecisionC;
            PrecisionM = config.PrecisionM;
            PrecisionZ = config.PrecisionZ;

            ColorP1 = config.ColorP1;
            ColorP3 = config.ColorP3;
            ColorP5 = config.ColorP5;
            ColorP10 = config.ColorP10;
            ColorP25 = config.ColorP25;
            ColorP50 = config.ColorP50;
            ColorP85 = config.ColorP85;
            ThickP1 = config.ThickP1;
            ThickP3 = config.ThickP3;
            ThickP5 = config.ThickP5;
            ThickP10 = config.ThickP10;
            ThickP25 = config.ThickP25;
            ThickP50 = config.ThickP50;
            ThickP85 = config.ThickP85;

            Color3SD = config.Color3SD;
            Color2SD = config.Color2SD;
            Color1SD = config.Color1SD;
            ColorMedian = config.ColorMedian;
            ColorNormalCurve = config.ColorNormalCurve;
            Thick3SD = config.Thick3SD;
            Thick2SD = config.Thick2SD;
            Thick1SD = config.Thick1SD;
            ThickMedian = config.ThickMedian;
            ThickNormalCurve = config.ThickNormalCurve;

            ColorPatientCurve = config.ColorPatientCurve;
            ColorPatientDataPointBorder = config.ColorPatientDataPointBorder;
            ColorPatientDataPointFill = config.ColorPatientDataPointFill;
            ThickPatientCurve = config.ThickPatientCurve;
            ThickPatientDataPointBorder = config.ThickPatientDataPointBorder;

            PatientDataPointSize = config.PatientDataPointSize;
            TransparentDataPoints = config.TransparentDataPoints;

            ShowLegend = config.ShowLegend;
            ShowDataPointLabels = config.ShowDataPointLabels;
            ShowCoordinateCursor = config.ShowCoordinateCursor;

            recentFiles = config.recentFiles;            
        }
        #endregion Constructors

        #region Properties
        #region Public Properties
        /// <summary>
        /// Gets/sets the culture code
        /// </summary>
        public string CultureCode
        {
            get
            {
                return cultureCode;
            }
            set
            {
                if (string.IsNullOrEmpty(value) || value.Contains("\\") || value.Contains("/"))
                    cultureCode = "en-US";
                else
                    cultureCode = value;
            }
        }
        
        /// <summary>
        /// Gets/sets whether to use color-coded output
        /// </summary>
        public string ColorCoding
        {
            get
            {
                return colorCoding;
            }
            set
            {
                if (string.IsNullOrEmpty(value))
                    colorCoding = "0";
                else
                    colorCoding = value;
            }
        }

        /// <summary>
        /// Gets/sets the reference type; e.g. CDC 2000, WHO 2007, etc.
        /// </summary>
        public string ReferenceType
        {
            get
            {
                return referenceType;
            }
            set
            {
                referenceType = value;
            }
        }

        /// <summary>
        /// Gets/sets the units used to measure weight.
        /// </summary>
        public string UnitsWeight
        {
            get
            {
                return unitsWeight;
            }
            set
            {
                if (value.Equals(MeasurementUnits.Pound.Abbreviation))
                    unitsWeight = value;
                else
                    unitsWeight = MeasurementUnits.Kilogram.Abbreviation;
            }
        }

        /// <summary>
        /// Gets/sets the units used to measure height.
        /// </summary>
        public string UnitsHeight
        {
            get
            {
                return unitsHeight;
            }
            set
            {
                if (value.Equals(MeasurementUnits.Inch.Abbreviation))
                    unitsHeight = value;
                else
                    unitsHeight = MeasurementUnits.Centimeter.Abbreviation;
            }
        }

        /// <summary>
        /// Gets/sets the units used to measure arm circumference.
        /// </summary>
        public string UnitsArm
        {
            get
            {
                return unitsArm;
            }
            set
            {
                if (value.Equals(MeasurementUnits.Inch.Abbreviation))
                    unitsArm = value;
                else
                    unitsArm = MeasurementUnits.Centimeter.Abbreviation;
            }
        }

        /// <summary>
        /// Gets/sets the units used to measure head circumference.
        /// </summary>
        public string UnitsHead
        {
            get
            {
                return unitsHead;
            }
            set
            {
                if (value.Equals(MeasurementUnits.Inch.Abbreviation))
                    unitsHead = value;
                else
                    unitsHead = MeasurementUnits.Centimeter.Abbreviation;
            }
        }

        /// <summary>
        /// Gets/sets the units used to measure skin folds.
        /// </summary>
        public string UnitsSkin
        {
            get
            {
                return unitsSkin;
            }
            set
            {
                if (value.Equals(MeasurementUnits.Inch.Abbreviation))
                    unitsSkin = value;
                else
                    unitsSkin = MeasurementUnits.Millimeter.Abbreviation;
            }
        }

        /// <summary>
        /// Gets/sets whether or not the delete button should be enabled.
        /// </summary>
        public bool DeleteButton
        {
            get
            {
                return deleteButton;
            }
            set
            {
                deleteButton = value;
            }
        }

        /// <summary>
        /// Gets/sets whether or not the survey fields should be enabled.
        /// </summary>
        public bool SurveyFields
        {
            get
            {
                return surveyFields;
            }
            set
            {
                surveyFields = value;
            }
        }

        /// <summary>
        /// Gets/sets the overall type of measurement system being used.
        /// </summary>
        public string MeasurementSystem
        {
            get
            {
                return measurementSystem;
            }
            set
            {
                if (value.Equals("metric") || value.Equals("customary"))
                    measurementSystem = value;
                else
                    measurementSystem = "metric";
            }
        }

        /// <summary>
        /// Gets/sets the decimal precision of z-scores
        /// </summary>
        public int PrecisionZ 
        {
            get 
            {
                return precisionZ;
            }
            set 
            {
                if(value >= 0 && value <= 5)
                    precisionZ = value;
                else
                    precisionZ = 3;
            }
        }

        /// <summary>
        /// Gets/sets the decimal precision of percentiles
        /// </summary>
        public int PrecisionC 
        {
            get 
            {
                return precisionC;
            }
            set 
            {                
                if(value >= 0 && value <= 5)
                    precisionC = value;
                else
                    precisionC = 2;
            }
        }

        /// <summary>
        /// Gets/sets the decimal precision of percents of median
        /// </summary>
        public int PrecisionM
        {
            get
            {
                return precisionM;
            }
            set
            {
                if (value >= 0 && value <= 5)
                    precisionM = value;
                else
                    precisionM = 2;
            }
        }

        /// <summary>
        /// Gets/sets whether or not the Name fields should be visible
        /// </summary>
        public bool Name
        {
            get
            {
                return name;
            }
            set
            {
                name = value;
            }
        }

        /// <summary>
        /// Gets/sets whether or not the birthdate field should be visible
        /// </summary>
        public bool Birthdate
        {
            get
            {
                return birthdate;
            }
            set
            {
                birthdate = value;
            }
        }

        /// <summary>
        /// Gets/sets whether or not the age field should be visible
        /// </summary>
        public bool Age
        {
            get
            {
                return age;
            }
            set
            {
                age = value;
            }
        }

        /// <summary>
        /// Gets/sets whether or not the mother field should be visible
        /// </summary>
        public bool Mother
        {
            get
            {
                return mother;
            }
            set
            {
                mother = value;
            }
        }

        /// <summary>
        /// Gets/sets whether or not the father field should be visible
        /// </summary>
        public bool Father
        {
            get
            {
                return father;
            }
            set
            {
                father = value;
            }
        }

        /// <summary>
        /// Gets/sets whether or not the address field should be visible
        /// </summary>
        public bool Address
        {
            get
            {
                return address;
            }
            set
            {
                address = value;
            }
        }

        /// <summary>
        /// Gets/sets whether or not the height measurement field should be visible
        /// </summary>
        public bool Height
        {
            get
            {
                return height;
            }
            set
            {
                height = value;
            }
        }

        /// <summary>
        /// Gets/sets whether or not the weight measurement field should be visible
        /// </summary>
        public bool Weight
        {
            get
            {
                return weight;
            }
            set
            {
                weight = value;
            }
        }

        /// <summary>
        /// Gets/sets whether or not the edema field should be visible
        /// </summary>
        public bool Edema
        {
            get
            {
                return edema;
            }
            set
            {
                edema = value;
            }
        }

        /// <summary>
        /// Gets/sets whether or not the head circumference measurement field should be visible
        /// </summary>
        public bool HeadCircumference
        {
            get
            {
                return headCircumference;
            }
            set
            {
                headCircumference = value;
            }
        }

        /// <summary>
        /// Gets/sets whether or not the arm circumference measurement field should be visible
        /// </summary>
        public bool ArmCircumference
        {
            get
            {
                return armCircumference;
            }
            set
            {
                armCircumference = value;
            }
        }

        /// <summary>
        /// Gets/sets whether or not the subscapular skin fold measurement field should be visible
        /// </summary>
        public bool SubscapularSkinFold
        {
            get
            {
                return subscapularSkinFold;
            }
            set
            {
                subscapularSkinFold = value;
            }
        }

        /// <summary>
        /// Gets/sets whether or not the triceps skin fold field measurement should be visible
        /// </summary>
        public bool TricepsSkinFold
        {
            get
            {
                return tricepsSkinFold;
            }
            set
            {
                tricepsSkinFold = value;
            }
        }

        /// <summary>
        /// Gets/sets whether or not the notes field should be visible
        /// </summary>
        public bool Notes
        {
            get
            {
                return notes;
            }
            set
            {
                notes = value;
            }
        }

        /// <summary>
        /// Gets/sets whether or not the height-age results field(s) should be visible
        /// </summary>
        public bool ResultsHA
        {
            get
            {
                return resultsHA;
            }
            set
            {
                resultsHA = value;
            }
        }

        /// <summary>
        /// Gets/sets whether or not the weight-age results field(s) should be visible
        /// </summary>
        public bool ResultsWA
        {
            get
            {
                return resultsWA;
            }
            set
            {
                resultsWA = value;
            }
        }

        /// <summary>
        /// Gets/sets whether or not the weight-height results field(s) should be visible
        /// </summary>
        public bool ResultsWH
        {
            get
            {
                return resultsWH;
            }
            set
            {
                resultsWH = value;
            }
        }

        /// <summary>
        /// Gets/sets whether or not the body mass index results field(s) should be visible
        /// </summary>
        public bool ResultsBMI
        {
            get
            {
                return resultsBMI;
            }
            set
            {
                resultsBMI = value;
            }
        }

        /// <summary>
        /// Gets/sets whether or not the flag field(s) should be visible
        /// </summary>
        public bool ResultsFlag
        {
            get
            {
                return resultsFlag;
            }
            set
            {
                resultsFlag = value;
            }
        }

        /// <summary>
        /// Gets/sets whether or not the legacy MUAC fields should be visible
        /// </summary>
        public bool ResultsLegacyArm
        {
            get
            {
                return resultsLegacyArm;
            }
            set
            {
                resultsLegacyArm = value;
            }
        }

        /// <summary>
        /// Gets/sets whether or not the head circumference results field(s) should be visible
        /// </summary>
        public bool ResultsHead
        {
            get
            {
                return resultsHead;
            }
            set
            {
                resultsHead = value;
            }
        }

        /// <summary>
        /// Gets/sets whether or not the MUAC results field(s) should be visible
        /// </summary>
        public bool ResultsArm
        {
            get
            {
                return resultsArm;
            }
            set
            {
                resultsArm = value;
            }
        }

        /// <summary>
        /// Gets/sets whether or not the SSF-age results field(s) should be visible
        /// </summary>
        public bool ResultsSSF
        {
            get
            {
                return resultsSSF;
            }
            set
            {
                resultsSSF = value;
            }
        }

        /// <summary>
        /// Gets/sets whether or not the TSF-age results field(s) should be visible
        /// </summary>
        public bool ResultsTSF
        {
            get
            {
                return resultsTSF;
            }
            set
            {
                resultsTSF = value;
            }
        }

        /// <summary>
        /// Gets/sets whether or not the percentiles column should be visible
        /// </summary>
        public bool StatsPercentiles
        {
            get
            {
                return statsPercentiles;
            }
            set
            {
                statsPercentiles = value;
            }
        }

        /// <summary>
        /// Gets/sets whether or not the z-scores column should be visible
        /// </summary>
        public bool StatsZScores
        {
            get
            {
                return statsZScores;
            }
            set
            {
                statsZScores = value;
            }
        }

        /// <summary>
        /// Gets/sets whether or not the percent of median column should be visible
        /// </summary>
        public bool StatsPercentOfMedian
        {
            get
            {
                return statsPercentOfMedian;
            }
            set
            {
                statsPercentOfMedian = value;
            }
        }


        /// <summary>
        /// Gets/sets the color of the 1st/99th percentile curves
        /// </summary>
        public string ColorP1
        {
            get
            {
                return colorP1;
            }
            set
            {
                colorP1 = value;
            }
        }

        /// <summary>
        /// Gets/sets the color of the 3rd/97th percentile curves
        /// </summary>
        public string ColorP3
        {
            get
            {
                return colorP3;
            }
            set
            {
                colorP3 = value;
            }
        }

        /// <summary>
        /// Gets/sets the color of the 5th/95th percentile curves
        /// </summary>
        public string ColorP5
        {
            get
            {
                return colorP5;
            }
            set
            {
                colorP5 = value;
            }
        }

        /// <summary>
        /// Gets/sets the color of the 10th/90th percentile curves
        /// </summary>
        public string ColorP10
        {
            get
            {
                return colorP10;
            }
            set
            {
                colorP10 = value;
            }
        }

        /// <summary>
        /// Gets/sets the color of the 25th/75th percentile curves
        /// </summary>
        public string ColorP25
        {
            get
            {
                return colorP25;
            }
            set
            {
                colorP25 = value;
            }
        }


        /// <summary>
        /// Gets/sets the color of the 50th percentile curve
        /// </summary>
        public string ColorP50
        {
            get
            {
                return colorP50;
            }
            set
            {
                colorP50 = value;
            }
        }

        /// <summary>
        /// Gets/sets the color of the 85th percentile curve
        /// </summary>
        public string ColorP85
        {
            get
            {
                return colorP85;
            }
            set
            {
                colorP85 = value;
            }
        }


        /// <summary>
        /// Gets/sets the color of the patient curve
        /// </summary>
        public string ColorPatientCurve
        {
            get
            {
                return colorPatientCurve;
            }
            set
            {
                colorPatientCurve = value;
            }
        }

        /// <summary>
        /// Gets/sets the color of the borders for each data point along the patient curve
        /// </summary>
        public string ColorPatientDataPointBorder
        {
            get
            {
                return colorPatientDataPointBorder;
            }
            set
            {
                colorPatientDataPointBorder = value;
            }
        }

        /// <summary>
        /// Gets/sets the color for each data point along the patient curve
        /// </summary>
        public string ColorPatientDataPointFill
        {
            get
            {
                return colorPatientDataPointFill;
            }
            set
            {
                colorPatientDataPointFill = value;
            }
        }

        /// <summary>
        /// Gets/sets the color for the +/- 3 SD curves
        /// </summary>
        public string Color3SD
        {
            get
            {
                return color3SD;
            }
            set
            {
                color3SD = value;
            }
        }

        /// <summary>
        /// Gets/sets the color for the +/- 2 SD curves
        /// </summary>
        public string Color2SD
        {
            get
            {
                return color2SD;
            }
            set
            {
                color2SD = value;
            }
        }

        /// <summary>
        /// Gets/sets the color for the +/- 1 SD curves
        /// </summary>
        public string Color1SD
        {
            get
            {
                return color1SD;
            }
            set
            {
                color1SD = value;
            }
        }

        /// <summary>
        /// Gets/sets the color for the median curve
        /// </summary>
        public string ColorMedian
        {
            get
            {
                return colorMedian;
            }
            set
            {
                colorMedian = value;
            }
        }

        /// <summary>
        /// Gets/sets the color for the normal curve
        /// </summary>
        public string ColorNormalCurve
        {
            get
            {
                return colorNormalCurve;
            }
            set
            {
                colorNormalCurve = value;
            }
        }

        /// <summary>
        /// Gets/sets the thickness of the 1st/99th percentile curves
        /// </summary>
        public int ThickP1
        {
            get
            {
                return thickP1;
            }
            set
            {
                thickP1 = value;
            }
        }

        /// <summary>
        /// Gets/sets the thickness of the 3rd/97th percentile curves
        /// </summary>
        public int ThickP3
        {
            get
            {
                return thickP3;
            }
            set
            {
                thickP3 = value;
            }
        }

        /// <summary>
        /// Gets/sets the thickness of the 5th/95th percentile curves
        /// </summary>
        public int ThickP5
        {
            get
            {
                return thickP5;
            }
            set
            {
                thickP5 = value;
            }
        }

        /// <summary>
        /// Gets/sets the thickness of the 10th/90th percentile curves
        /// </summary>
        public int ThickP10
        {
            get
            {
                return thickP10;
            }
            set
            {
                thickP10 = value;
            }
        }

        /// <summary>
        /// Gets/sets the thickness of the 25th/75th percentile curves
        /// </summary>
        public int ThickP25
        {
            get
            {
                return thickP25;
            }
            set
            {
                thickP25 = value;
            }
        }


        /// <summary>
        /// Gets/sets the thickness of the 50th percentile curve
        /// </summary>
        public int ThickP50
        {
            get
            {
                return thickP50;
            }
            set
            {
                thickP50 = value;
            }
        }

        /// <summary>
        /// Gets/sets the thickness of the 85th percentile curve
        /// </summary>
        public int ThickP85
        {
            get
            {
                return thickP85;
            }
            set
            {
                thickP85 = value;
            }
        }

        /// <summary>
        /// Gets/sets the thickness of the patient curve
        /// </summary>
        public int ThickPatientCurve
        {
            get
            {
                return thickPatientCurve;
            }
            set
            {
                thickPatientCurve = value;
            }
        }        

        /// <summary>
        /// Gets/sets the thickness of the borders for each data point along the patient curve
        /// </summary>
        public double ThickPatientDataPointBorder
        {
            get
            {
                return thickPatientDataPointBorder;
            }
            set
            {
                thickPatientDataPointBorder = value;
            }
        }

        /// <summary>
        /// Gets/sets the thickness for the +/- 3 SD curves
        /// </summary>
        public int Thick3SD
        {
            get
            {
                return thick3SD;
            }
            set
            {
                thick3SD = value;
            }
        }

        /// <summary>
        /// Gets/sets the thickness for the +/- 2 SD curves
        /// </summary>
        public int Thick2SD
        {
            get
            {
                return thick2SD;
            }
            set
            {
                thick2SD = value;
            }
        }

        /// <summary>
        /// Gets/sets the thickness for the +/- 1 SD curves
        /// </summary>
        public int Thick1SD
        {
            get
            {
                return thick1SD;
            }
            set
            {
                thick1SD = value;
            }
        }

        /// <summary>
        /// Gets/sets the thickness for the median curve
        /// </summary>
        public int ThickMedian
        {
            get
            {
                return thickMedian;
            }
            set
            {
                thickMedian = value;
            }
        }

        /// <summary>
        /// Gets/sets the thickness for the normal curve
        /// </summary>
        public int ThickNormalCurve
        {
            get
            {
                return thickNormalCurve;
            }
            set
            {
                thickNormalCurve = value;
            }
        }

        /// <summary>
        /// Gets/sets the size of data points on the graph
        /// </summary>
        public int PatientDataPointSize
        {
            get
            {
                return patientDataPointSize;
            }
            set
            {
                patientDataPointSize = value;
            }
        }

        /// <summary>
        /// Gets/sets whether patient data points should be transparent
        /// </summary>
        public bool TransparentDataPoints
        {
            get
            {
                return transparentDataPoints;
            }
            set
            {
                transparentDataPoints = value;
            }
        }

        public bool ShowLegend
        {
            get
            {
                return showLegend;
            }
            set
            {
                showLegend = value;
            }
        }

        public bool ShowDataPointLabels
        {
            get
            {
                return showDataPointLabels;
            }
            set
            {
                showDataPointLabels = value;
            }
        }

        public bool ShowCoordinateCursor
        {
            get
            {
                return showCoordinateCursor;
            }
            set
            {
                showCoordinateCursor = value;
            }
        }
        #endregion Public Properties

        #region Protected Properties
        #endregion Protected Properties

        #region Private Properties
        #endregion Private Properties
        #endregion Properties

        #region Methods
        #region Public Methods
        /// <summary>
        /// Sets the current directory.
        /// </summary>                
        public static void SetCurrentDirectory(string directory)
        {
            #region Input Validation
            // Input validation
            if (directory.Contains("*") || directory.Contains("?") || directory.Contains("\"") || directory.Contains("<") || directory.Contains(">") || directory.Contains("|")) 
            {
                throw new Exception("Parameter out of bounds in SetCurrentDirectory method. Value: " + directory);
            }
            #endregion

            currentDirectory = directory;
        }

        /// <summary>
        /// Gets the current directory.
        /// </summary>        
        /// <returns>The current directory</returns>
        public static string GetCurrentDirectory()
        {
            return currentDirectory;
        }

        /// <summary>
        /// Loads the configuration XML file.
        /// </summary>        
        /// <returns>Whether or not the file load was successful.</returns>
        internal bool LoadConfigFile()
        {
            try
            {
                TextReader reader = new StreamReader("NutriStat.Config.xml");

                string line = "";

                int recentFileNumber = 0;

                while ((line = reader.ReadLine()) != null)
                {
                    // Store recent files for later in case we need to
                    // re-write the config file.
                    if (line.Contains("<recentfile>") && recentFileNumber < 4)
                    {
                        string temp = ConfigValueParser(line);
                        recentFileNumber++;
                        if (temp != string.Empty)
                        {
                            bool add = true;
                            for (int i = 0; i < recentFiles.Count; i++)
                            {
                                if (recentFiles[i].Equals(temp))
                                {
                                    add = false;
                                }
                            }
                            if (add)
                            {
                                recentFiles.Add(temp);
                            }
                        }

                    }

                    if (line.Contains("<colorCoding>"))
                    {
                        string color = ConfigValueParser(line);
                        ColorCoding = color;
                    }

                    // Get precision
                    if (line.Contains("<precisionz>"))
                        PrecisionZ = Convert.ToInt32(ConfigValueParser(line));

                    if (line.Contains("<precisionc>"))
                        PrecisionC = Convert.ToInt32(ConfigValueParser(line));

                    if (line.Contains("<precisionm>"))
                        PrecisionM = Convert.ToInt32(ConfigValueParser(line));

                    if (line.Contains("<measurement>"))
                    {
                        string measurement = ConfigValueParser(line);
                        if (measurement.Equals("metric"))
                            MeasurementSystem = "metric";
                        else
                            MeasurementSystem = "customary";
                    }

                    if (line.Contains("<reference>"))
                    {
                        string reference = ConfigValueParser(line);
                        if (reference.Equals(NutriStat.Statistics.GrowthReferenceTypes.WHO1978))
                            ReferenceType = NutriStat.Statistics.GrowthReferenceTypes.WHO1978;
                        else if (reference.Equals(NutriStat.Statistics.GrowthReferenceTypes.WHO2006))
                            ReferenceType = NutriStat.Statistics.GrowthReferenceTypes.WHO2006;
                        else if (reference.Equals(NutriStat.Statistics.GrowthReferenceTypes.WHO2007))
                            ReferenceType = NutriStat.Statistics.GrowthReferenceTypes.WHO2007;
                        else
                            ReferenceType = NutriStat.Statistics.GrowthReferenceTypes.CDC2000;
                    }

                    if (line.Contains("<enableDelete>"))
                    {
                        string delete = ConfigValueParser(line);
                        if (delete.Equals("true"))
                            DeleteButton = true;
                        else
                            DeleteButton = false;
                    }

                    if (line.Contains("<showSurveyFields>"))
                    {
                        string survey = ConfigValueParser(line);
                        if (survey.Equals("true"))
                            SurveyFields = true;
                        else
                            SurveyFields = false;
                    }

                    if (line.Contains("<name>"))
                    {
                        string value = ConfigValueParser(line);
                        if (value.Equals("true"))
                            Name = true;
                        else
                            Name = false;
                    }

                    if (line.Contains("<birthdate>"))
                    {
                        string value = ConfigValueParser(line);
                        if (value.Equals("true"))
                            Birthdate = true;
                        else
                            Birthdate = false;
                    }

                    if (line.Contains("<age>"))
                    {
                        string value = ConfigValueParser(line);
                        if (value.Equals("true"))
                            Age = true;
                        else
                            Age = false;
                    }

                    if (line.Contains("<edema>"))
                    {
                        string value = ConfigValueParser(line);
                        if (value.Equals("true"))
                            Edema = true;
                        else
                            Edema = false;
                    }

                    if (line.Contains("<notes>"))
                    {
                        string value = ConfigValueParser(line);
                        if (value.Equals("true"))
                            Notes = true;
                        else
                            Notes = false;
                    }

                    if (line.Contains("<heightAge>"))
                    {
                        string value = ConfigValueParser(line);
                        if (value.Equals("true"))
                            ResultsHA = true;
                        else
                            ResultsHA = false;
                    }

                    if (line.Contains("<weightAge>"))
                    {
                        string value = ConfigValueParser(line);
                        if (value.Equals("true"))
                            ResultsWA = true;
                        else
                            ResultsWA = false;
                    }

                    if (line.Contains("<weightHeight>"))
                    {
                        string value = ConfigValueParser(line);
                        if (value.Equals("true"))
                            ResultsWH = true;
                        else
                            ResultsWH = false;
                    }

                    if (line.Contains("<BMI>"))
                    {
                        string value = ConfigValueParser(line);
                        if (value.Equals("true"))
                            ResultsBMI = true;
                        else
                            ResultsBMI = false;
                    }

                    if (line.Contains("<headCircumference>"))
                    {
                        string value = ConfigValueParser(line);
                        if (value.Equals("true"))
                            ResultsHead = true;
                        else
                            ResultsHead = false;
                    }

                    if (line.Contains("<MUAC>"))
                    {
                        string value = ConfigValueParser(line);
                        if (value.Equals("true"))
                        {
                            ResultsArm = true;
                        }
                        else
                        {
                            ResultsArm = false;
                        }
                    }

                    if (line.Contains("<MUACLegacy>"))
                    {
                        string value = ConfigValueParser(line);
                        if (value.Equals("true"))
                        {
                            ResultsLegacyArm = true;
                        }
                        else
                        {
                            ResultsLegacyArm = false;
                        }
                    }

                    if (line.Contains("<SSFAge>"))
                    {
                        string value = ConfigValueParser(line);
                        if (value.Equals("true"))
                        {
                            ResultsSSF = true;
                        }
                        else
                        {
                            ResultsSSF = false;
                        }
                    }

                    if (line.Contains("<TSFAge>"))
                    {
                        string value = ConfigValueParser(line);
                        if (value.Equals("true"))
                        {
                            ResultsTSF = true;
                        }
                        else
                        {
                            ResultsTSF = false;
                        }
                    }

                    if (line.Contains("<head>"))
                    {
                        string value = ConfigValueParser(line);
                        if (value.Equals("true"))
                        {
                            HeadCircumference = true;
                        }
                        else
                        {
                            HeadCircumference = false;
                        }
                    }

                    if (line.Contains("<arm>"))
                    {
                        string value = ConfigValueParser(line);
                        if (value.Equals("true"))
                        {
                            ArmCircumference = true;
                        }
                        else
                        {
                            ArmCircumference = false;
                        }
                    }

                    if (line.Contains("<ssf>"))
                    {
                        string value = ConfigValueParser(line);
                        if (value.Equals("true"))
                        {
                            SubscapularSkinFold = true;
                        }
                        else
                        {
                            SubscapularSkinFold = false;
                        }
                    }

                    if (line.Contains("<tsf>"))
                    {
                        string value = ConfigValueParser(line);
                        if (value.Equals("true"))
                        {
                            TricepsSkinFold = true;
                        }
                        else
                        {
                            TricepsSkinFold = false;
                        }
                    }

                    if (line.Contains("<height>"))
                    {
                        string value = ConfigValueParser(line);
                        if (value.Equals("true"))
                        {
                            Height = true;
                        }
                        else
                        {
                            Height = false;
                        }
                    }

                    if (line.Contains("<weight>"))
                    {
                        string value = ConfigValueParser(line);
                        if (value.Equals("true"))
                        {
                            Weight = true;
                        }
                        else
                        {
                            Weight = false;
                        }
                    }

                    if (line.Contains("<percentile>"))
                    {
                        string value = ConfigValueParser(line);
                        if (value.Equals("true"))
                            StatsPercentiles = true;
                        else
                            StatsPercentiles = false;
                    }

                    if (line.Contains("<zscore>"))
                    {
                        string value = ConfigValueParser(line);
                        if (value.Equals("true"))
                            StatsZScores = true;
                        else
                            StatsZScores = false;
                    }

                    if (line.Contains("<percentMedian>"))
                    {
                        string value = ConfigValueParser(line);
                        if (value.Equals("true"))
                            StatsPercentOfMedian = true;
                        else
                            StatsPercentOfMedian = false;
                    }

                    if (line.Contains("<flag>"))
                    {
                        string value = ConfigValueParser(line);
                        if (value.Equals("true"))
                        {
                            ResultsFlag = true;
                        }
                        else
                        {
                            ResultsFlag = false;
                        }
                    }

                    if (line.Contains("<unitsWeight>"))
                    {
                        string value = ConfigValueParser(line);
                        if (value.Equals(MeasurementUnits.Kilogram.Abbreviation))
                        {
                            UnitsWeight = MeasurementUnits.Kilogram.Abbreviation;
                        }
                        else
                        {
                            UnitsWeight = MeasurementUnits.Pound.Abbreviation;
                        }
                    }

                    if (line.Contains("<unitsHeight>"))
                    {
                        string value = ConfigValueParser(line);
                        if (value.Equals(MeasurementUnits.Centimeter.Abbreviation))
                        {
                            UnitsHeight = MeasurementUnits.Centimeter.Abbreviation;
                        }
                        else
                        {
                            UnitsHeight = MeasurementUnits.Inch.Abbreviation;
                        }
                    }

                    if (line.Contains("<unitsArm>"))
                    {
                        string value = ConfigValueParser(line);
                        if (value.Equals(MeasurementUnits.Centimeter.Abbreviation))
                        {
                            UnitsArm = MeasurementUnits.Centimeter.Abbreviation;
                        }
                        else
                        {
                            UnitsArm = MeasurementUnits.Inch.Abbreviation;
                        }
                    }

                    if (line.Contains("<unitsHead>"))
                    {
                        string value = ConfigValueParser(line);
                        if (value.Equals(MeasurementUnits.Centimeter.Abbreviation))
                        {
                            UnitsHead = MeasurementUnits.Centimeter.Abbreviation;
                        }
                        else
                        {
                            UnitsHead = MeasurementUnits.Inch.Abbreviation;
                        }
                    }

                    if (line.Contains("<unitsSkin>"))
                    {
                        string value = ConfigValueParser(line);
                        if (value.Equals(MeasurementUnits.Millimeter.Abbreviation))
                        {
                            UnitsSkin = MeasurementUnits.Millimeter.Abbreviation;
                        }
                        else
                        {
                            UnitsSkin = MeasurementUnits.Inch.Abbreviation;
                        }
                    }

                    if (line.Contains("<colorP1>"))
                    {
                        string value = ConfigValueParser(line);
                        ColorP1 = value;                        
                    }
                    if (line.Contains("<colorP3>"))
                    {
                        string value = ConfigValueParser(line);
                        ColorP3 = value;
                    }
                    if (line.Contains("<colorP5>"))
                    {
                        string value = ConfigValueParser(line);
                        ColorP5 = value;
                    }
                    if (line.Contains("<colorP10>"))
                    {
                        string value = ConfigValueParser(line);
                        ColorP10 = value;
                    }
                    if (line.Contains("<colorP25>"))
                    {
                        string value = ConfigValueParser(line);
                        ColorP25 = value;
                    }
                    if (line.Contains("<colorP50>"))
                    {
                        string value = ConfigValueParser(line);
                        ColorP50 = value;
                    }
                    if (line.Contains("<colorP85>"))
                    {
                        string value = ConfigValueParser(line);
                        ColorP85 = value;
                    }
                    if (line.Contains("<colorPatientCurve>"))
                    {
                        string value = ConfigValueParser(line);
                        ColorPatientCurve = value;
                    }
                    if (line.Contains("<colorPatientDataPointBorder>"))
                    {
                        string value = ConfigValueParser(line);
                        ColorPatientDataPointBorder = value;
                    }
                    if (line.Contains("<colorPatientDataPointFill>"))
                    {
                        string value = ConfigValueParser(line);
                        ColorPatientDataPointFill = value;
                    }

                    if (line.Contains("<color3SD>"))
                    {
                        string value = ConfigValueParser(line);
                        Color3SD = value;
                    }
                    if (line.Contains("<color2SD>"))
                    {
                        string value = ConfigValueParser(line);
                        Color2SD = value;
                    }
                    if (line.Contains("<color1SD>"))
                    {
                        string value = ConfigValueParser(line);
                        Color1SD = value;
                    }
                    if (line.Contains("<colorMedian>"))
                    {
                        string value = ConfigValueParser(line);
                        ColorMedian = value;
                    }
                    if (line.Contains("<colorNormalCurve>"))
                    {
                        string value = ConfigValueParser(line);
                        ColorNormalCurve = value;
                    }

                    if (line.Contains("<thickP1>"))
                    {
                        string value = ConfigValueParser(line);
                        ThickP1 = Convert.ToInt32(ConfigValueParser(line));
                    }
                    if (line.Contains("<thickP3>"))
                    {
                        string value = ConfigValueParser(line);
                        ThickP3 = Convert.ToInt32(ConfigValueParser(line));
                    }
                    if (line.Contains("<thickP5>"))
                    {
                        string value = ConfigValueParser(line);
                        ThickP5 = Convert.ToInt32(ConfigValueParser(line));
                    }
                    if (line.Contains("<thickP10>"))
                    {
                        string value = ConfigValueParser(line);
                        ThickP10 = Convert.ToInt32(ConfigValueParser(line));
                    }
                    if (line.Contains("<thickP25>"))
                    {
                        string value = ConfigValueParser(line);
                        ThickP25 = Convert.ToInt32(ConfigValueParser(line));
                    }
                    if (line.Contains("<thickP50>"))
                    {
                        string value = ConfigValueParser(line);
                        ThickP50 = Convert.ToInt32(ConfigValueParser(line));
                    }
                    if (line.Contains("<thickP85>"))
                    {
                        string value = ConfigValueParser(line);
                        ThickP85 = Convert.ToInt32(ConfigValueParser(line));
                    }
                    if (line.Contains("<thickPatientCurve>"))
                    {
                        string value = ConfigValueParser(line);
                        ThickPatientCurve = Convert.ToInt32(ConfigValueParser(line));
                    }
                    if (line.Contains("<thickPatientDataPointBorder>"))
                    {
                        string value = ConfigValueParser(line);
                        ThickPatientDataPointBorder = Convert.ToDouble(ConfigValueParser(line));
                    }

                    if (line.Contains("<thick3SD>"))
                    {
                        string value = ConfigValueParser(line);
                        Thick3SD = Convert.ToInt32(ConfigValueParser(line));
                    }
                    if (line.Contains("<thick2SD>"))
                    {
                        string value = ConfigValueParser(line);
                        Thick2SD = Convert.ToInt32(ConfigValueParser(line));
                    }
                    if (line.Contains("<thick1SD>"))
                    {
                        string value = ConfigValueParser(line);
                        Thick1SD = Convert.ToInt32(ConfigValueParser(line));
                    }
                    if (line.Contains("<thickMedian>"))
                    {
                        string value = ConfigValueParser(line);
                        ThickMedian = Convert.ToInt32(ConfigValueParser(line));
                    }
                    if (line.Contains("<thickNormalCurve>"))
                    {
                        string value = ConfigValueParser(line);
                        ThickNormalCurve = Convert.ToInt32(ConfigValueParser(line));
                    }

                    if (line.Contains("<patientDataPointSize>"))
                    {
                        string value = ConfigValueParser(line);
                        PatientDataPointSize = Convert.ToInt32(ConfigValueParser(line));
                    }

                    if (line.Contains("<dataPointTransparent>"))
                    {
                        string value = ConfigValueParser(line);
                        if (value.Equals("true"))
                        {
                            TransparentDataPoints = true;
                        }
                        else
                        {
                            TransparentDataPoints = false;
                        }
                    }

                    if (line.Contains("<showLegend>"))
                    {
                        string value = ConfigValueParser(line);
                        if (value.Equals("true"))
                        {
                            ShowLegend = true;
                        }
                        else
                        {
                            ShowLegend = false;
                        }
                    }

                    if (line.Contains("<showDataPointLabels>"))
                    {
                        string value = ConfigValueParser(line);
                        if (value.Equals("true"))
                        {
                            ShowDataPointLabels = true;
                        }
                        else
                        {
                            ShowDataPointLabels = false;
                        }
                    }

                    if (line.Contains("<showCoordinateCursor>"))
                    {
                        string value = ConfigValueParser(line);
                        if (value.Equals("true"))
                        {
                            ShowCoordinateCursor = true;
                        }
                        else
                        {
                            ShowCoordinateCursor = false;
                        }
                    }

                }
                reader.Close();
                reader.Dispose();
            }
            catch (FileNotFoundException ex)
            {                
                MessageBox.Show(ex.ToString());
                WriteDefaultConfigFile();                
                return false;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());                
                return false;
            }
            return true;
        } // end LoadConfigFile()

        /// <summary>
        /// Creates a new configuration file.
        /// </summary>
        /// <param name="path">The path of the table to input into the array</param>
        internal void AddDataTable(string path)
        {
            #region Input validation
            if (path.Length > 1024)
            {
                throw new Exception("Parameter out of bounds.");
            }           
            
            // If it's already in the array, don't add it. We don't want duplicates.
            for (int i = 0; i < recentFiles.Count; i++)
            {
                if (recentFiles[i].Equals(path))
                {
                    return;
                }
            }
            #endregion Input Validation
            
            recentFiles.Insert(0, path);

            try
            {
                if (recentFiles.Count > 4)
                {
                    recentFiles.RemoveAt(4);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

            WriteConfigFile();            
        }

        /// <summary>
        /// Creates a new configuration file.
        /// </summary>
        /// <returns>Whether or not the file write was successful.</returns>
        internal bool WriteConfigFile()
        {
            try
            {
                if (File.Exists("NutriStat.Config.xml"))
                {
                    //MessageBox.Show("Found config file; deleting.");
                    File.Delete("NutriStat.Config.xml");
                }

                // set up stream
                TextWriter writer = new StreamWriter("NutriStat.Config.xml");

                writer.WriteLine("<?xml version=\"1.0\" standalone=\"yes\" ?>");
                writer.WriteLine("<config>");
                writer.WriteLine("  <settings> ");
                writer.WriteLine("    <version NutriStatVersion=\"0.1\" ConfigVersion=\"001\" />");
                writer.WriteLine("    <databases>");

                for (int i = 0; i < this.recentFiles.Count; i++)
                {
                    writer.WriteLine("      <recentfile>" + recentFiles[i] + "</recentfile>");
                }

                writer.WriteLine("    </databases>");
                writer.WriteLine("    <language>en-US</language>");
                writer.WriteLine("    <colorCoding>1</colorCoding>");
                writer.WriteLine("    <precisionz>" + PrecisionZ.ToString() + "</precisionz>");
                writer.WriteLine("    <precisionc>" + PrecisionC.ToString() + "</precisionc>");
                writer.WriteLine("    <precisionm>2</precisionm>");

                //if (MeasurementSystem.Equals("customary") || MeasurementSystem.Equals("Customary"))
                //    writer.WriteLine("    <measurement>customary</measurement>");
                //else
                    writer.WriteLine("    <measurement>metric</measurement>");

                writer.WriteLine("    <unitsWeight>" + UnitsWeight + "</unitsWeight>");
                writer.WriteLine("    <unitsHeight>" + UnitsHeight + "</unitsHeight>");
                writer.WriteLine("    <unitsArm>" + UnitsArm + "</unitsArm>");
                writer.WriteLine("    <unitsHead>" + UnitsHead + "</unitsHead>");
                writer.WriteLine("    <unitsSkin>" + UnitsSkin + "</unitsSkin>");

                writer.WriteLine("    <reference>" + ReferenceType + "</reference>");

                if (DeleteButton)
                    writer.WriteLine("    <enableDelete>true</enableDelete>");
                else
                    writer.WriteLine("    <enableDelete>false</enableDelete>");

                if (SurveyFields)
                    writer.WriteLine("    <showSurveyFields>true</showSurveyFields>");
                else
                    writer.WriteLine("    <showSurveyFields>false</showSurveyFields>");

                writer.WriteLine("    <dataEntryFields>");

                if (Name)
                    writer.WriteLine("      <name>true</name>");
                else
                    writer.WriteLine("      <name>false</name>");

                if (Birthdate)
                    writer.WriteLine("      <birthdate>true</birthdate>");
                else
                    writer.WriteLine("      <birthdate>false</birthdate>");

                if (Age)
                    writer.WriteLine("      <age>true</age>");
                else
                    writer.WriteLine("      <age>false</age>");

                if (Height)
                    writer.WriteLine("      <height>true</height>");
                else
                    writer.WriteLine("      <height>false</height>");

                if (Weight)
                    writer.WriteLine("      <weight>true</weight>");
                else
                    writer.WriteLine("      <weight>false</weight>");

                if (Edema)
                    writer.WriteLine("      <edema>true</edema>");
                else
                    writer.WriteLine("      <edema>false</edema>");

                if (HeadCircumference)
                    writer.WriteLine("      <head>true</head>");
                else
                    writer.WriteLine("      <head>false</head>");

                if (ArmCircumference)
                    writer.WriteLine("      <arm>true</arm>");
                else
                    writer.WriteLine("      <arm>false</arm>");

                if (SubscapularSkinFold)
                    writer.WriteLine("      <ssf>true</ssf>");
                else
                    writer.WriteLine("      <ssf>false</ssf>");

                if (TricepsSkinFold)
                    writer.WriteLine("      <tsf>true</tsf>");
                else
                    writer.WriteLine("      <tsf>false</tsf>");

                if (Notes)
                    writer.WriteLine("      <notes>true</notes>");
                else
                    writer.WriteLine("      <notes>false</notes>");

                writer.WriteLine("    </dataEntryFields>");
                writer.WriteLine("    <resultFields>");

                if (ResultsHA)
                    writer.WriteLine("      <heightAge>true</heightAge>");
                else
                    writer.WriteLine("      <heightAge>false</heightAge>");

                if (ResultsWA)
                    writer.WriteLine("      <weightAge>true</weightAge>");
                else
                    writer.WriteLine("      <weightAge>false</weightAge>");

                if (ResultsWH)
                    writer.WriteLine("      <weightHeight>true</weightHeight>");
                else
                    writer.WriteLine("      <weightHeight>false</weightHeight>");

                if (ResultsFlag)
                    writer.WriteLine("      <flag>true</flag>");
                else
                    writer.WriteLine("      <flag>false</flag>");

                if (ResultsBMI)
                    writer.WriteLine("      <BMI>true</BMI>");
                else
                    writer.WriteLine("      <BMI>false</BMI>");

                if (ResultsHead)
                    writer.WriteLine("      <headCircumference>true</headCircumference>");
                else
                    writer.WriteLine("      <headCircumference>false</headCircumference>");

                if (ResultsArm)
                    writer.WriteLine("      <MUAC>true</MUAC>");
                else
                    writer.WriteLine("      <MUAC>false</MUAC>");

                if (ResultsLegacyArm)
                    writer.WriteLine("      <MUACLegacy>true</MUACLegacy>");
                else
                    writer.WriteLine("      <MUACLegacy>false</MUACLegacy>");

                if (ResultsSSF)
                    writer.WriteLine("      <SSFAge>true</SSFAge>");
                else
                    writer.WriteLine("      <SSFAge>false</SSFAge>");

                if (ResultsTSF)
                    writer.WriteLine("      <TSFAge>true</TSFAge>");
                else
                    writer.WriteLine("      <TSFAge>false</TSFAge>");

                writer.WriteLine("      <statistics>");

                if (StatsPercentiles)
                    writer.WriteLine("        <percentile>true</percentile>");
                else
                    writer.WriteLine("        <percentile>false</percentile>");

                if (StatsZScores)
                    writer.WriteLine("        <zscore>true</zscore>");
                else
                    writer.WriteLine("        <zscore>false</zscore>");

                if (StatsPercentOfMedian)
                    writer.WriteLine("        <percentMedian>true</percentMedian>");
                else
                    writer.WriteLine("        <percentMedian>false</percentMedian>");

                writer.WriteLine("      </statistics>");
                writer.WriteLine("    </resultFields>");

                writer.WriteLine("    <graphing>");
                writer.WriteLine("    </graphing>");
                writer.WriteLine("      <colorP1>" + ColorP1 + "</colorP1>");
                writer.WriteLine("      <colorP3>" + ColorP3 + "</colorP3>");
                writer.WriteLine("      <colorP5>" + ColorP5 + "</colorP5>");
                writer.WriteLine("      <colorP10>" + ColorP10 + "</colorP10>");
                writer.WriteLine("      <colorP25>" + ColorP25 + "</colorP25>");
                writer.WriteLine("      <colorP50>" + ColorP50 + "</colorP50>");
                writer.WriteLine("      <colorP85>" + ColorP85 + "</colorP85>");
                writer.WriteLine("      <colorPatientCurve>" + ColorPatientCurve + "</colorPatientCurve>");
                writer.WriteLine("      <colorPatientDataPointBorder>" + ColorPatientDataPointBorder + "</colorPatientDataPointBorder>");
                writer.WriteLine("      <colorPatientDataPointFill>" + ColorPatientDataPointFill + "</colorPatientDataPointFill>");
                writer.WriteLine("      <color3SD>" + Color3SD + "</color3SD>");
                writer.WriteLine("      <color2SD>" + Color2SD + "</color2SD>");
                writer.WriteLine("      <color1SD>" + Color1SD + "</color1SD>");
                writer.WriteLine("      <colorMedian>" + ColorMedian + "</colorMedian>");
                writer.WriteLine("      <colorNormalCurve>" + ColorNormalCurve + "</colorNormalCurve>");
                writer.WriteLine("      <thickP1>" + ThickP1.ToString() + "</thickP1>");
                writer.WriteLine("      <thickP3>" + ThickP3.ToString() + "</thickP3>");
                writer.WriteLine("      <thickP5>" + ThickP5.ToString() + "</thickP5>");
                writer.WriteLine("      <thickP10>" + ThickP10.ToString() + "</thickP10>");
                writer.WriteLine("      <thickP25>" + ThickP25.ToString() + "</thickP25>");
                writer.WriteLine("      <thickP50>" + ThickP50.ToString() + "</thickP50>");
                writer.WriteLine("      <thickP85>" + ThickP85.ToString() + "</thickP85>");
                writer.WriteLine("      <thickPatientCurve>" + ThickPatientCurve.ToString() + "</thickPatientCurve>");
                writer.WriteLine("      <thickPatientDataPointBorder>" + ThickPatientDataPointBorder.ToString() + "</thickPatientDataPointBorder>");
                writer.WriteLine("      <thick3SD>" + Thick3SD.ToString() + "</thick3SD>");
                writer.WriteLine("      <thick2SD>" + Thick2SD.ToString() + "</thick2SD>");
                writer.WriteLine("      <thick1SD>" + Thick1SD.ToString() + "</thick1SD>");
                writer.WriteLine("      <thickMedian>" + ThickMedian.ToString() + "</thickMedian>");
                writer.WriteLine("      <thickNormalCurve>" + ThickNormalCurve.ToString() + "</thickNormalCurve>");
                writer.WriteLine("      <patientDataPointSize>" + PatientDataPointSize.ToString() + "</patientDataPointSize>");
                if (TransparentDataPoints)
                {
                    writer.WriteLine("      <dataPointTransparent>true</dataPointTransparent>");
                }
                else
                {
                    writer.WriteLine("      <dataPointTransparent>false</dataPointTransparent>");
                }

                if (ShowLegend)
                {
                    writer.WriteLine("      <showLegend>true</showLegend>");
                }
                else
                {
                    writer.WriteLine("      <showLegend>false</showLegend>");
                }

                if (ShowDataPointLabels)
                {
                    writer.WriteLine("      <showDataPointLabels>true</showDataPointLabels>");
                }
                else
                {
                    writer.WriteLine("      <showDataPointLabels>false</showDataPointLabels>");
                }

                if (ShowCoordinateCursor)
                {
                    writer.WriteLine("      <showCoordinateCursor>true</showCoordinateCursor>");
                }
                else
                {
                    writer.WriteLine("      <showCoordinateCursor>false</showCoordinateCursor>");
                }

                writer.WriteLine("  </settings>");
                writer.WriteLine("</config>");

                writer.Close();
                writer.Dispose();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
                return false;
            }

            return true;

            // TODO: Use System.XML for this, not text writers
        } // end WriteConfigFile()
        #endregion Public Methods

        #region Protected Methods
        #endregion Protected Methods

        #region Private Methods
        /// <summary>
        /// Separates the data from the XML tags in the configuration file.
        /// </summary>
        /// <param name="line">A line of text from the configuration XML file</param>
        /// <returns>Just the data inbetween the XML tags contained in the input parameter</returns>
        private string ConfigValueParser(string line)
        {
            #region Input Validation
            if(line.Length > 1024) 
            {
                throw new Exception("Parameter out of bounds.");
            }
            #endregion Input Validation

            int last = line.LastIndexOf("<");
            int first = 1 + line.IndexOf(">");
            string value = line.Substring(first, last - first);
            value.TrimStart(' ');
            value.TrimEnd(' ');

            return value;
        } // end ConfigValueParser()

        /// <summary>
        /// Creates a default, baseline configuration file. This should only happen if there
        /// is no config file currently available.
        /// </summary>        
        /// <returns>Whether or not the file write was successful.</returns>
        private bool WriteDefaultConfigFile()
        {
            TextWriter writer = new StreamWriter("NutriStat.Config.xml");

            try
            {
                writer.WriteLine("<?xml version=\"1.0\" standalone=\"yes\" ?>");
                writer.WriteLine("<config>");
                writer.WriteLine("  <settings> ");
                writer.WriteLine("    <version NutriStatVersion=\"0.1\" ConfigVersion=\"001\" />");
                writer.WriteLine("    <databases>");
                //writer.WriteLine("      <recentfile name="C:\Epi_Info\Nutri.mdb" table="nutChildren" />");
                writer.WriteLine("    </databases>");
                writer.WriteLine("    <language>en-US</language>");
                writer.WriteLine("    <colorCoding>1</colorCoding>");                
                writer.WriteLine("    <precisionz>3</precisionz>");
                writer.WriteLine("    <precisionc>2</precisionc>");
                writer.WriteLine("    <precisionm>2</precisionm>");
                writer.WriteLine("    <measurement>metric</measurement>");
                writer.WriteLine("    <unitsWeight>lbs</unitsWeight>");
                writer.WriteLine("    <unitsHeight>in</unitsHeight>");
                writer.WriteLine("    <unitsArm>in</unitsArm>");
                writer.WriteLine("    <unitsHead>in</unitsHead>");
                writer.WriteLine("    <unitsSkin>in</unitsSkin>");
                writer.WriteLine("    <reference>CDC</reference>");
                writer.WriteLine("    <enableDelete>false</enableDelete>");
                writer.WriteLine("    <dataEntryFields>");
                writer.WriteLine("      <name>true</name>");
                writer.WriteLine("      <birthdate>true</birthdate>");
                writer.WriteLine("      <age>true</age>");
                writer.WriteLine("      <height>true</height>");
                writer.WriteLine("      <weight>true</weight>");
                writer.WriteLine("      <edema>true</edema>");
                writer.WriteLine("      <head>true</head>");
                writer.WriteLine("      <arm>true</arm>");
                writer.WriteLine("      <ssf>true</ssf>");
                writer.WriteLine("      <tsf>true</tsf>");
                writer.WriteLine("      <notes>true</notes>");
                writer.WriteLine("    </dataEntryFields>");
                writer.WriteLine("    <resultFields>");
                writer.WriteLine("      <heightAge>true</heightAge>");
                writer.WriteLine("      <weightAge>true</weightAge>");
                writer.WriteLine("      <weightHeight>true</weightHeight>");
                writer.WriteLine("      <flag>true</flag>");
                writer.WriteLine("      <BMI>true</BMI>");
                writer.WriteLine("      <headCircumference>true</headCircumference>");
                writer.WriteLine("      <MUAC>true</MUAC>");
                writer.WriteLine("      <MUACLegacy>false</MUACLegacy>");
                writer.WriteLine("      <SSFAge>false</SSFAge>");
                writer.WriteLine("      <TSFAge>false</TSFAge>");
                writer.WriteLine("      <statistics>");
                writer.WriteLine("        <percentile>true</percentile>");
                writer.WriteLine("        <zscore>true</zscore>");
                writer.WriteLine("        <percentMedian>false</percentMedian>");
                writer.WriteLine("      </statistics>");
                writer.WriteLine("    </resultFields>");
                writer.WriteLine("    <graphing>");
                writer.WriteLine("      <colorP1>128,0,0</colorP1>");
                writer.WriteLine("      <colorP3>255,0,0</colorP3>");
                writer.WriteLine("      <colorP5>255,165,0</colorP5>");
                writer.WriteLine("      <colorP10>238,130,238</colorP10>");
                writer.WriteLine("      <colorP25>0,128,0</colorP25>");
                writer.WriteLine("      <colorP50>0,0,255</colorP50>");
                writer.WriteLine("      <colorP85>165,42,42</colorP85>");
                writer.WriteLine("      <colorPatientCurve>0,0,0</colorPatientCurve>");
                writer.WriteLine("      <colorPatientDataPointBorder>0,0,0</colorPatientDataPointBorder>");
                writer.WriteLine("      <colorPatientDataPointFill>0,0,0</colorPatientDataPointFill>");
                writer.WriteLine("      <color3SD>255,0,0</color3SD>");
                writer.WriteLine("      <color2SD>255,165,0</color2SD>");
                writer.WriteLine("      <color1SD>0,128,0</color1SD>");
                writer.WriteLine("      <colorMedian>0,0,255</colorMedian>");
                writer.WriteLine("      <colorNormalCurve>100,149,237</colorNormalCurve>");
                writer.WriteLine("      <thickP1>0</thickP1>");
                writer.WriteLine("      <thickP3>1</thickP3>");
                writer.WriteLine("      <thickP5>1</thickP5>");
                writer.WriteLine("      <thickP10>1</thickP10>");
                writer.WriteLine("      <thickP25>1</thickP25>");
                writer.WriteLine("      <thickP50>2</thickP50>");
                writer.WriteLine("      <thickP85>1</thickP85>");
                writer.WriteLine("      <thickPatientCurve>1</thickPatientCurve>");
                writer.WriteLine("      <thickPatientDataPointBorder>1</thickPatientDataPointBorder>");                
                writer.WriteLine("      <thick3SD>2</thick3SD>");
                writer.WriteLine("      <thick2SD>2</thick2SD>");
                writer.WriteLine("      <thick1SD>2</thick1SD>");
                writer.WriteLine("      <thickMedian>2</thickMedian>");
                writer.WriteLine("      <thickNormalCurve>2</thickNormalCurve>");
                writer.WriteLine("      <patientDataPointSize>8</patientDataPointSize>");
                writer.WriteLine("      <dataPointTransparent>false</dataPointTransparent>");
                writer.WriteLine("      <showLegend>true</dataPointTransparent>");
                writer.WriteLine("      <showDataPointLabels>true</showDataPointLabels>");
                writer.WriteLine("      <showCoordinateCursor>true</showCoordinateCursor>");
                writer.WriteLine("    </graphing>");
                writer.WriteLine("  </settings>");
                writer.WriteLine("</config>");
            }
            catch
            {
                return false;
            }
            finally
            {
                writer.Close();
                writer.Dispose();
            }
            return true;
        } // end WriteDefaultConfigFile()
        #endregion Private Methods
        #endregion Methods
    }
}
